import plugin from '../../../lib/plugins/plugin.js'
import { Config } from '../components/index.js'
import { createRequire } from 'module'
import YAML from "yaml";
const require = createRequire(import.meta.url)
const fs =require('fs')

var ban = Config.callme.ban

//bot名字
const botname = global.Bot.nickname;

export class call_me_please extends plugin {
constructor() {
super({
name: '我的新昵称',
dsc: '让bot对你换个称呼',
event: 'message',
priority: 25,
rule: [
{
reg: `^(${botname})?(请)?(叫|喊)(我)(.*)`,
fnc: 'call_me_please'
},
{
reg:`^(${botname})?(请)?(叫|喊)(他|她|它)(.*)$`,
fnc:`call_ta_please`
},
{
reg: `^(.+)?(${botname})?(我)(是)(谁|哪个|哪位|什么|啥)(.+)?$`,
fnc: 'call_me_now'
},
{
reg:`^(.+)?(${botname})?(他|她|它|TA|ta)(是)(谁|哪个|哪位|什么|啥)(.+)?$`,
fnc:'call_ta_now'
},
{
reg:'^(允许|禁止)被动改名',
fnc:'call_me_switch'
},
{
reg:'^#*清除昵称|删除昵称$',
fnc:'dont_call_me'
}
]
})
}

//xx请叫我
async call_me_please(e) {
let config = await Config.admin_set
let isplugin = config.callme
if (!isplugin) return false
let uid = e.msg.replace(/铃岚请叫我|铃岚请喊我|铃岚叫我|铃岚喊我|请叫我|请喊我|叫我|喊我/g,"").trim(); //此处自行修改
if (ban.some(item => uid.includes(item))) {
let msg = `不可以教坏${botname}`
await e.reply(msg)
} else {
if (uid.length >= 20) {
let msg= `名字太长，${botname}记不住..`
await e.reply(msg)
} else {
await e.reply('好～')
await redis.set(`call_me_now${e.user_id}`,uid)
}
}
}

//xx请叫ta
async call_ta_please(e) {
let config = await Config.admin_set
let isplugin = config.callme
if (!isplugin) return false
if (!e.atBot) {
} else {
let msg = `${botname}就是${botname}喔~`
await e.reply(msg)
} if (!e.at) {
} else {
let uid = e.msg.replace(/铃岚请喊他|铃岚叫他|铃岚喊他|铃岚请叫她|铃岚请喊她|铃岚叫她|铃岚喊她|铃岚请叫它|铃岚请喊它|铃岚叫它|铃岚叫它|请喊他|叫他|喊他|请叫她|请喊她|叫她|喊她|请叫它|请喊它|叫它|叫它/g,"")//此处自行修改
if(e.atall){
let msg = '不可以随意艾特全体成员喔'
await e.reply(msg)
} else {
if (ban.some(item => uid.includes(item))) {
let msg = `不可以教坏${botname}`
await e.reply(msg)
} else {
if (uid.length >= 20) {
let msg = `名字太长，${botname}记住...`
await e.reply(msg)
} else {
e.user_id = e.at
if (await redis.get(`call_me_switch${e.user_id}`) == 1) {
let key = await redis.get(`call_me_now${e.user_id}`)
if (key == null) {
await redis.set(`call_me_now${e.user_id}`,uid)
await e.reply('好~')
} else {
await e.reply('TA已经有昵称了')
}
} else {
let msg = '不可以改TA的昵称..'
await e.reply(msg)
}
}
}
}
}
}
    
//我是谁
async call_me_now(e) {
let config = await Config.admin_set
let isplugin = config.callme
if (!isplugin) return false
let uid = await redis.get(`call_me_now${e.user_id}`)
if (uid == null){
uid = e.nickname;
}
e.reply(`是${uid}~`)
return true;
}

//ta是谁
async call_ta_now(e) {
let config = await Config.admin_set
let isplugin = config.callme
if (!isplugin) return false
if (!e.atBot) {
} else {
await e.reply(`是${botname}~`)
}
if (!e.at) {
return true
} else {
if(e.atall){
let msg = '不可以随意艾特全体成员喔'
await e.reply(msg)
} else {
e.user_id = e.at
let uid = await redis.get(`call_me_now${e.user_id}`)
if (uid == null) {
uid = e.nickname;
}
await e.reply(`是${uid}~`)
}
}
}

//允许|禁止 被动改名
async call_me_switch(e) {
let config = await Config.admin_set
let isplugin = config.callme
if (!isplugin) return false
let match_obj = e.msg.replace(/被动改名/g,'')
if (match_obj == '允许') {
await redis.set(`call_me_switch${e.user_id}`,'1')
await e.reply('好~')
}
if (match_obj == '禁止') {
await redis.set(`call_me_switch${e.user_id}`,'2')
await e.reply('好~')
}
}
 
/** 清除昵称 */
async dont_call_me(e) {
let config = await Config.admin_set
let isplugin = config.callme
if (!isplugin) return false
await redis.del(`call_me_now${e.user_id}`)
await e.reply('已恢复昵称~')
}
}